<?php

/**
 * This is the model class for table "a_category".
 *
 * The followings are the available columns in table 'a_category':
 * @property string $id
 * @property string $parent
 * @property string $a_category_name
 * @property string $url_key
 * @property string $hits
 * @property string $sorder
 * @property integer $status
 */
class ACategory extends ACategoryPeer
{
	var $className = __CLASS__;
    
    var $children;
    var $parentName;
    public $_arrCategory = array();
    
	/**
	 * Returns the static model of the specified AR class.
	 * @return ACategory the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
            array('url_key', 'buildUrlKey'),
			array('status', 'numerical', 'integerOnly'=>true),
			array('parent, hits, sorder', 'length', 'max'=>20),
			array('a_category_name, url_key', 'length', 'max'=>255),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, parent, a_category_name, url_key, hits, sorder, status', 'safe', 'on'=>'search'),
		);
	}
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);

		$criteria->compare('parent',$this->parent,true);

		$criteria->compare('a_category_name',$this->a_category_name,true);

		$criteria->compare('url_key',$this->url_key,true);

		$criteria->compare('hits',$this->hits,true);

		$criteria->compare('sorder',$this->sorder,true);

		$criteria->compare('status',$this->status);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
    public function rebuildSOrder() {
        $criteria=new CDbCriteria;
        $criteria->select = "id, sorder";
        $criteria->order = "sorder, id";
        $data = $this->model()->findAll($criteria);
        $row_numbers = count($data);
        $sorder = 0;
        for($i=0; $i<$row_numbers-1; $i++) {
            $sorder++;
            self::model()->updateByPk($data[$i]->id, array('sorder'=>$sorder));
        }
        $sorder++;
        self::model()->updateByPk($data[$i]->id, array('sorder'=>$sorder));
    }
    public function buildUrlKey() {
        if($this->url_key == "") $this->url_key = Common::makeFriendlyUrl($this->a_category_name, true);
        else $this->url_key = Common::makeFriendlyUrl($this->url_key, true);
        $record = Lvc::selectOne($this->tableName(), 'id', 'url_key="'.$this->url_key.'" ');
        if($record)
            $this->url_key .= uniqid();
    }
}